package xyf.p10.dp;

import commons.TreeNode;

/**
 * ClassName: P0337_Rob
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:27 AM
 * Version: 1.0
 **/
public class P0337_Rob
{
    public int rob(TreeNode root)
    {
        int[] res = dfs(root);
        return Math.max(res[0], res[1]);
    }

    private int[] dfs(TreeNode root)
    {
        if (root == null)
        {
            return new int[]{0, 0};
        }

        int[] left = dfs(root.left);
        int[] right = dfs(root.right);

        int[] res = new int[2];
        res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        res[1] = root.val + left[0] + right[0];
        return res;
    }
}